var express = require("express");
var router = express.Router();
var multiparty = require("multiparty");
var { messageModel, cronyModel } = require("../model/rxh");

router.get("/searchlist", async (req, res) => {
  const name = req.query.name;
  const data = await cronyModel.find({ name: name });
  console.log(data);
  res.send({
    code: 200,
    msg: "发送成功",
    data,
  });
});

router.get("/cronylist", async (req, res) => {
  const id = req.query.id;
  const data = await cronyModel.find({ _id: id });
  // 获取所有的消息列表
  const lists = data[0].cronylist;
  const cronylists = [];
  // 获取的是你登录之后，这个人所对应的你的好友
  // console.log(lists,'所有');
  // 证明有好友
  if (lists.length > 0) {
    for (let i = 0; i < lists.length; i++) {
      //查找有多少个好友？？？
      const cronylist = await cronyModel.find(
        { _id: lists[i] },
        { _id: 1, name: 1, avatar: 1 }
      );
      //   这个是你的账号是谁，就是谁的信息
      //   console.log(cronylist,7777777);
      //查找好友之间互相发送的消息 并根据发送时间进行排序
      const message = await messageModel
        .find({ cronys: { $all: [id, String(lists[i])] } })
        .sort({ updatedAt: 1 }); //排序
      // console.log(message,44444444);
      //   这是你的好友列表的详细信息
      const obj = { ...cronylist[0]._doc };
      // console.log(obj,33333);
      message.forEach((item) => {
        if (item.sender != id) {
          obj["message"] = item["message"];
          obj["date"] = item["createdAt"];
        }
      });
      cronylists.push(obj);
    }
  }
  res.send({
    code: 200,
    msg: "发送成功",
    data,
    cronylists,
  });
});

// 获取所有的聊天消息-你们两个的所有聊天信息
router.post("/messagelist", async (req, res) => {
  const { id, toId } = req.body;
  const messagelist = await messageModel
    .find({ cronys: { $all: [id, toId] } })
    .sort({ updatedAt: 1 });
  const data = messagelist.map((item) => {
    return {
      isMy: item.sender.toString() === id,
      message: item.message,
      updatedAt: item.updatedAt,
      basevideo: item.basevideo,
      voicetime: item.voicetime,
      time:item.time,
      name:item.name,
      address:item.address,
      latitude:item.latitude,
      longitude:item.longitude,
      types:item.types,
      xls:item.xls
    };
  });
  // console.log(data);
  res.send({
    code: 200,
    msg: "接收成功",
    data,
    
  });
});




// 获取单个好友的信息,也就是右侧的页面展示的好友备注以及头像
router.get("/getCrony", async (req, res) => {
  const toId = req.query.id;
  const data = await cronyModel.find({ _id: toId });
  // console.log(data[0]);
  res.send({
    code: 200,
    msg: "查询成功",
    data: data[0],
  });
});

// 发送消息
router.post("/addMessage", async (req, res) => {
  const body = req.body;
  const data = await messageModel.create(body);
  // console.log(data, "发送成功7777777777777");
  res.send({
    code: 200,
    msg: "发送成功",
    data,
  });
});

router.post("/deng", async (req, res) => {
  const name = req.body.name;
  const password = req.body.password;
  const data = await cronyModel.find();
  const istrue = data.some((item) => {
    return (item = name === name && item.password === password);
  });
  if (istrue) {
    const user = data.find((item) => {
      return item.name === name && item.password === password;
    });
    res.send({
      code: 200,
      msg: "登录成功",
      user,
    });
  } else {
    res.send({
      code: 201,
    });
  }
});

// router.post("/upload", (req, res) => {
//   const from = new multiparty.Form();
//   from.uploadDir = "upload";
//   from.parse(req, (err, formData, excelData) => {
//     console.log(formData, excelData);
    
//   });
// });

module.exports = router;
